﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Inventory.Business.API;
using Inventory.Business.DomainObjects;
using System.Threading;

namespace Inventory.Test
{
    /// <summary>
    /// Summary description for TransactionCodeGenerator
    /// </summary>
    [TestClass]
    public class TransactionCodeGeneratorTest
    {
        [TestMethod]
        public void RollbackTest()
        {
            var code = new TransactionCode();
            var icom = TransactionCodeGenerator.GetCode(code, TransactionDirection.Out, 75);
            icom.Rollback();

            var code2 = new TransactionCode();
            icom = TransactionCodeGenerator.GetCode(code2, TransactionDirection.Out, 75);
            icom.Rollback();

            Assert.AreEqual(code.ToString(), code2.ToString());
            Console.WriteLine(String.Format("RolebackTest\r\ncode1:\t{0}\r\ncode2:\t{1}\r\n", code.ToString(), code2.ToString()));
        }

        [TestMethod]
        public void CommitTest()
        {
            var code = new TransactionCode();
            var icom = TransactionCodeGenerator.GetCode(code, TransactionDirection.Out, 75);
            icom.Commit();

            var code2 = new TransactionCode();
            icom = TransactionCodeGenerator.GetCode(code2, TransactionDirection.Out, 75);
            icom.Rollback();

            Assert.AreNotEqual(code.ToString(), code2.ToString());
            Console.WriteLine(String.Format("CommitTest\r\ncode1:\t{0}\r\ncode2:\t{1}", code.ToString(), code2.ToString()));
        }

        [TestMethod]
        public void MultiCodeGenerationRequestsTest()
        {
            for (int i = 0; i < 100; i++)
            {
                Console.WriteLine("Trial #: {0}" + Environment.NewLine, i);
                var code = new TransactionCode();
                var icom = TransactionCodeGenerator.GetCode(code, TransactionDirection.In, 75);
                icom.Commit();

                var code2 = new TransactionCode();
                icom = TransactionCodeGenerator.GetCode(code2, TransactionDirection.In, 75);
                icom.Rollback();

                Assert.AreNotEqual(code.ToString(), code2.ToString());
                Console.WriteLine(String.Format("CommitTest\r\ncode1:\t{0}\r\ncode2:\t{1}", code.ToString(), code2.ToString()));

            }
        }
    }
}
